#include "VSearchDlg.h"
#include <qfile.h>
#include <qtextstream.h>
#include <qdebug.h>
#include <qlistview.h>
#include <qpushbutton.h>
#include <QDebug>
#include <QKeyEvent>
#include <QDockWidget>

#include "VFunctions.h"

VSearchDlg::VSearchDlg(QWidget *parent)
    : QFrame(parent)
{
	ui.setupUi(this);
    Qt::WindowFlags m_flags = windowFlags();
    setWindowFlags(m_flags | Qt::WindowStaysOnTopHint);
    _timer.setSingleShot(true);
    connect(&_timer, &QTimer::timeout, this, &VSearchDlg::on_timerout);
}

VSearchDlg::~VSearchDlg()
{
}

void VSearchDlg::slot_show_text(QString text, int time)
{
    QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
    if(dw == nullptr) return;
    dw->setWindowTitle(text);
    _timer.start(time);
}

void VSearchDlg::on_btn_hide_clicked()
{
    QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
    if(dw == nullptr) return;
    dw->hide();
}

void VSearchDlg::on_radio_regexp_toggled(bool checked)
{
    if(checked)
    {
        ui.checkbox_whole_word->setChecked(false);
        ui.checkbox_whole_word->setEnabled(false);
        ui.checkbox_reverse->setChecked(false);
        ui.checkbox_reverse->setEnabled(false);
    }
    else
    {
        ui.checkbox_whole_word->setEnabled(true);
        ui.checkbox_reverse->setEnabled(true);
    }
}

void VSearchDlg::on_btn_find_clicked()
{
    if(ui.combobox_search->currentText().isEmpty()) return;
    on_timerout();
    emit signal_find(ui.combobox_search->currentText(),
                     ui.radio_regexp->isChecked(),
                     ui.checkbox_case->isChecked(),
                     ui.checkbox_whole_word->isChecked(),
                     ui.checkbox_while->isChecked(),
                     ui.checkbox_reverse->isChecked() == false,
                     -1, -1, true, false, ui.radio_regexp->isChecked());
    if(combobox_container(ui.combobox_search, ui.combobox_search->currentText()) == false)
        ui.combobox_search->addItem(ui.combobox_search->currentText());
}

void VSearchDlg::on_timerout()
{
    _timer.stop();
    QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
    if(dw == nullptr) return;
    dw->setWindowTitle("搜索");
}

bool VSearchDlg::combobox_container(QComboBox *combobox, QString text)
{
    for(int i=0 ; i<combobox->count(); i++)
    {
        if(combobox->itemText(i) == text) return true;
    }
    return false;
}

void VSearchDlg::on_btn_replace_clicked()
{
    if(ui.combobox_search->currentText().isEmpty()) return;
    if(ui.combobox_replace->currentText() == ui.combobox_search->currentText()) return;
    on_timerout();
    emit signal_find(ui.combobox_search->currentText(),
                     ui.radio_regexp->isChecked(),
                     ui.checkbox_case->isChecked(),
                     ui.checkbox_whole_word->isChecked(),
                     ui.checkbox_while->isChecked(),
                     ui.checkbox_reverse->isChecked() == false,
                     -1, -1, true, false, ui.radio_regexp->isChecked(),
                     ui.combobox_replace->currentText(), true);
    if(combobox_container(ui.combobox_search, ui.combobox_search->currentText()) == false)
        ui.combobox_search->addItem(ui.combobox_search->currentText());
    if(combobox_container(ui.combobox_replace, ui.combobox_replace->currentText()) == false)
        ui.combobox_replace->addItem(ui.combobox_replace->currentText());
}

void VSearchDlg::on_btn_replace_all_clicked()
{
    if(ui.combobox_search->currentText().isEmpty()) return;
    if(ui.combobox_replace->currentText() == ui.combobox_search->currentText()) return;
    on_timerout();
    emit signal_find(ui.combobox_search->currentText(),
                     ui.radio_regexp->isChecked(),
                     ui.checkbox_case->isChecked(),
                     ui.checkbox_whole_word->isChecked(),
                     ui.checkbox_while->isChecked(),
                     ui.checkbox_reverse->isChecked() == false,
                     -1, -1, true, false, ui.radio_regexp->isChecked(),
                     ui.combobox_replace->currentText(), false, true);
    if(combobox_container(ui.combobox_search, ui.combobox_search->currentText()) == false)
        ui.combobox_search->addItem(ui.combobox_search->currentText());
    if(combobox_container(ui.combobox_replace, ui.combobox_replace->currentText()) == false)
        ui.combobox_replace->addItem(ui.combobox_replace->currentText());
}
